import {EditableProTable, nanoid, PageContainer} from "@ant-design/pro-components";
import {ShowApi} from "@/services/School/SchoolApi";
import {Button, Card, DatePicker, Modal, Space, Table} from "antd";
import {history} from "umi";
import React, {useEffect, useState} from "react";
import {
  TjdBigAwardDetail, TjdBigPlatformDetail,
  TjdBigProjectDetail,
  TjdBigTeacherAwardDetail,
  TjdLeadersDetail
} from "@/services/Province/Dashboard";
import {EditOutlined} from "@ant-design/icons";
import {ProvinceDelete} from "@/services/Province/ProvinceApi";

const tableMap = {
  '国家级学术领军人才（含青年人才）情况': '3_2_2_1',
  '主持国家重点重大项目情况': '4_2_2_1',
  '学科国家级教学成果奖情况': '2_2_1_1',
  '教师国家级奖项情况': '4_1_1_1',
  '国家级平台建设情况': '5_2_1_1',
}

const columnsMap = {
  '3_2_2_1': [
    {
      title: '人才姓名',
      dataIndex: 'rc_name',
      formItemProps: () => {
        return {
          rules: [{required: true, message: '此项为必填项'}]
        };
      },
    },
    {
      title: '荣誉称号',
      dataIndex: 'honor_name',
      formItemProps: () => {
        return {
          rules: [{required: true, message: '此项为必填项'}]
        };
      },
    },
    {
      title: '获批年度',
      dataIndex: 'yr',
      formItemProps: () => {
        return {
          rules: [{required: true, message: '此项为必填项'}]
        };
      },
      renderFormItem: () => <DatePicker picker="year"/>,
    },
  ],
  '4_2_2_1': [
    {
      title: '年度',
      dataIndex: 'yr',
      width: '15%',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      renderFormItem: () => <DatePicker picker="year" />,
    },
    {
      title: '项目名称',
      dataIndex: 'proj_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '项目类型',
      dataIndex: 'proj_type',
      width: '12%',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      valueEnum: {
        纵向: '纵向',
        横向: '横向',
      },
    },
    {
      title: '项目起止年月',
      dataIndex: 'proj_fromto_ymth',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      renderFormItem: () => <DatePicker.RangePicker picker="month" />,
    },
    {
      title: '项目经费（万元）',
      dataIndex: 'proj_fund',
      width: '15%',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
  ],
  '2_2_1_1': [
    {
      title: '成果名称',
      dataIndex: 'award_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }]
        };
      },
    },
    {
      title: '奖项类型',
      dataIndex: 'award_type',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }]
        };
      },
      valueEnum: {
        '国家级高等教育教学成果奖': '国家级高等教育教学成果奖',
        '国家级基础教育教学成果奖': '国家级基础教育教学成果奖',
        '国家级职业教育教学成果奖': '国家级职业教育教学成果奖',
      },
    },
    {
      title: '奖项等级',
      dataIndex: 'award_level',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }]
        };
      },
      valueEnum: {
        '国家特等奖': '国家特等奖',
        '国家一等奖': '国家一等奖',
        '国家二等奖': '国家二等奖',
      },
    },
    {
      title: '成果完成人',
      dataIndex: 'tch_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }]
        };
      },
    },
    {
      title: '获奖时间',
      dataIndex: 'award_date',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }]
        };
      },
      renderFormItem: () => <DatePicker picker="month" />,
    },
  ],
  '4_1_1_1': [
    {
      title: '教师姓名',
      dataIndex: 'tch_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '所获奖项名称',
      dataIndex: 'award_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '所获奖项等级',
      dataIndex: 'grade',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      valueEnum: {
        特等奖: '特等奖',
        一等奖: '一等奖',
        二等奖: '二等奖',
        三等奖: '三等奖',
      },
    },
    {
      title: '评奖组织单位',
      dataIndex: 'award_eval_org',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '组织单位类型',
      dataIndex: 'award_eval_org_type',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '获奖年份',
      dataIndex: 'yr',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      renderFormItem: () => <DatePicker picker="year" />,
    },
  ],
  '5_2_1_1': [
    {
      title: '年度',
      dataIndex: 'yr',
      width: '180px',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      renderFormItem: () => <DatePicker picker="year" />,
    },
    {
      title: '平台名称',
      dataIndex: 'plat_name',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '平台级别',
      dataIndex: 'plat_level',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      valueEnum: {
        国家级: '国家级',
      },
    },
    {
      title: '批准时间',
      dataIndex: 'appro_date',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
      renderFormItem: () => <DatePicker picker="month" />,
    },
  ],
}

const addTableKey = (data: []) => {
  for (let i = 0; i < data.length; i++) {
    data[i]["tid"] = nanoid()
  }
}

export default () => {
  // todo: 根据学校+学科定位 columns 和 request 参数
  const url = history.location.pathname.split('/')
  const schoolSubject = url[url.length - 2]
  const table = url[url.length - 1]
  const download = tableMap[table]
  const columns = columnsMap[download];
  console.log("@url", url, schoolSubject, table, download)

  const deleteData = (row_id: []) => {
    ProvinceDelete(download, row_id, schoolSubject)
  }

  return <PageContainer title={schoolSubject +"-"+ table}>
    <Card>
      {/* 开始：详情表格 */}
      <EditableProTable
        pagination={{
          pageSize: 20
        }}
        request={async () => {
          return new Promise(resolve => {
            // ShowApi(download).then(response => {
            //   console.log("@response", response)
            //   if (response.status === 0) {
            //     resolve({data: response.data})
            //   }
            // })
            if (download === "3_2_2_1") {
              TjdLeadersDetail({detail: schoolSubject}).then(res => {
                console.log("@res", res)
                if (res.status === 0) {
                  addTableKey(res.data)
                  resolve({data: res.data})
                } else {
                  history.push("/")
                }
              })
            } else if (download === "4_2_2_1") {
              TjdBigProjectDetail({detail: schoolSubject}).then(res => {
                if (res.status === 0) {
                  addTableKey(res.data)
                  resolve({data: res.data})
                } else {
                  history.push("/")
                }
              })
            } else if (download === "2_2_1_1") {
              TjdBigAwardDetail({detail: schoolSubject}).then(res => {
                if (res.status === 0) {
                  addTableKey(res.data)
                  resolve({data: res.data})
                } else {
                  history.push("/")
                }
              })
            } else if (download === '4_1_1_1') {
              TjdBigTeacherAwardDetail({detail: schoolSubject}).then(res => {
                if (res.status === 0) {
                  addTableKey(res.data)
                  resolve({data: res.data})
                } else {
                  history.push("/")
                }
              })
            } else if (download === "5_2_1_1") {
              TjdBigPlatformDetail({detail: schoolSubject}).then(res => {
                if (res.status === 0) {
                  addTableKey(res.data)
                  resolve({data: res.data})
                } else {
                  history.push("/")
                }
              })
            }
          })
        }}
        rowSelection={{
          // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
          // 注释该行则默认不显示下拉选项
          selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
          defaultSelectedRowKeys: [],
        }}
        tableAlertOptionRender={(a) => {
          return (
            <Space size={16}>
              <Button onClick={() => {
                console.log(a)
                Modal.warning({
                  title: "请选中需要删除的数据！",
                  content: <>
                    <EditableProTable
                      pagination={{
                        pageSize: 20
                      }}
                      request={async () => {
                        return new Promise(resolve => {
                          resolve({data: a.selectedRows})
                        })
                      }}
                      columns={columns}
                      rowKey="tid"
                      style={{marginBottom: 20}}
                      recordCreatorProps={{
                        style: {
                          display: 'none',
                        },
                      }}
                      rowSelection={{
                        // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
                        // 注释该行则默认不显示下拉选项
                        selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
                        defaultSelectedRowKeys: [],
                      }}
                      tableAlertOptionRender={(a) => {
                        console.log(a)
                        return (
                          <Button danger onClick={() => {
                            Modal.confirm({
                              title: "确认删除数据？",
                              onOk() {
                                const id_arr = []
                                for (let i = 0; i < a.selectedRows.length; i++) {
                                  id_arr.push(a.selectedRows[i].id)
                                }
                                deleteData(id_arr)
                                window.location.reload()
                              }
                            })
                          }}>删除选中数据</Button>
                        )
                      }}
                    />
                  </>,
                  width: 1000,
                  okText: "关闭"
                })
              }}><EditOutlined /> 批量操作</Button>
            </Space>
          );
        }}
        columns={columns}
        rowKey="tid"
        style={{marginBottom: 20}}
        recordCreatorProps={{
          style: {
            display: 'none',
          },
        }}
      />
      {/* 结束：详情表格 */}

      <Button onClick={() => history.goBack()}>返回</Button>
    </Card>
  </PageContainer>
}
